/*
 * @Descripttion: 
 * @version: 
 * @Author: 尹伊
 * @Date: 2020-09-11 17:01:39
 * @LastEditors: 尹伊
 * @LastEditTime: 2020-09-11 17:48:43
 */
import {
  createWM,
  removeWM,
  hideWM,
  showWM
} from './watermark'
let factory = {}
factory.MoneyTest = /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/;

// 四舍五入
factory.keepCount = function (val, number) {
  return (Math.round(val * Math.pow(10, number)) / Math.pow(10, number)).toFixed(number)
}

function keepTwoDecimalFull(num) {
  var result = parseFloat(num);
  if (isNaN(result)) {
    console.error('参数非数值，无法四舍五入保留两位小数！');
    return false;
  }
  result = Math.round(num * 100) / 100;
  var s_x = result.toString();
  var pos_decimal = s_x.indexOf('.');
  if (pos_decimal < 0) {
    pos_decimal = s_x.length;
    s_x += '.';
  }
  while (s_x.length <= pos_decimal + 2) {
    s_x += '0';
  }
  return s_x;
}
// 金额添加千分位
factory.comdify = function (n) {
  if (!n) return n;
  let str = n.split('.');
  let re = /\d{1,3}(?=(\d{3})+$)/g;
  let n1 = str[0].replace(re, "$&,");
  console.log(str, re, n1)
  return str.length > 1 && str[1] ? `${n1}.${str[1]}` : `${n1}.00`;
};
//去除千分位中的‘，’
factory.delcommafy = function (num) {
  if (!num) return num;
  num = num.toString();
  num = num.replace(/,/gi, '');
  return num;
};
factory.valdateFn = function (rule, val, cb) {
  console.log(rule, '验证')
  setTimeout(() => {
    if (val) {
      let inputVal = factory.delcommafy(val);
      if (rule.test(inputVal)) {
        cb()
      } else {
        cb('只能是数字金额,最多两位小数')
      }
    }
    cb()
  })
}
// 验证金额数字可以为负数
factory.moneyValid = function (rule, val, cb) {
  factory.valdateFn(/((^-?[1-9]\d*)|^-?0)(\.\d{0,2}){0,1}$/, val, cb);
};
// 验证金额数字不可以为负数
factory.moneyNValid = function (val, cb) {
  console.log(cb, '记过')
  factory.valdateFn(factory.MoneyTest, val, cb);
};

// 获取输入框的值
factory.getInputValue = function (el) {
  let inputVal = el.target.value || '';
  return factory.comdify(factory.delcommafy(inputVal));
};

// 为指定容器元素加载水印
export function loadWM(waterMarkOptions, container) {
  if (!waterMarkOptions || !waterMarkOptions.watermarkSwitch) return
  const selector = '.' + (container.className.split(' ').join('.'))
  if (!container.querySelector('.__wm') || !window.watermarkList[selector]) {
    const mo = createWM({
      container,
      ...waterMarkOptions
    })
    if (!window.watermarkList) window.watermarkList = {}
    window.watermarkList[selector] = {
      mo,
      show: true
    }
  }
  for (const wmSelector in window.watermarkList) {
    if (wmSelector === selector) continue
    window.watermarkList[wmSelector].show = false
    const elItem = document.querySelector(wmSelector)
    if (elItem) hideWM(elItem, window.watermarkList[wmSelector].mo)
  }
}
// 为指定容器元素卸载水印
export function unloadWM(container) {
  const selector = '.' + (container.className.split(' ').join('.'))
  if (window.watermarkList && window.watermarkList[selector]) {
    removeWM(container, window.watermarkList[selector].mo)
    delete window.watermarkList[selector]

    for (const wmSelector in window.watermarkList) {
      window.watermarkList[wmSelector].show = false
      const elItem = document.querySelector(wmSelector)
      if (elItem) showWM(elItem, window.watermarkList[wmSelector].mo)
    }
  }
}
// 卸载所有水印
export function unloadAllWM() {
  for (const wmSelector in window.watermarkList) {
    const elItem = document.querySelector(wmSelector)
    if (elItem) removeWM(elItem, window.watermarkList[wmSelector].mo)
    delete window.watermarkList[wmSelector]
  }
}

export default factory
